/**
 * Created by CJB on 2017/2/22.
 */

(function(){
    var dept_user_setting = {
        check: {
            enable: true,
            chkboxType:  { "Y" : "ps", "N" : "ps" },
            radioType:'all'
        },
        view: {
            showLine:false,
            dblClickExpand: true,
            fontCss:setFontCss_ztree
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        callback: {
            //onClick:onTreeNodeClick,
        }
    };

    $(function(){
        //初始化添加窗口
        var addWindow = $("#userWindow").dialog({
            autoOpen: true,
            width: 675,
            height: 455,
            resizable: false,
            title: "选择用户列表",
            modal: true,
            open:function(){
                init();
            },
            close:function(){
                $("#userWindow").remove();
            }
        });
        addWindow.dialog('open');
        $("#loadDiv").loading().hideLoading();

    });

    function init(){
        setSubmitClick();
        if(callBackFun)
            callBackFun = eval(callBackFun);
        deptList.push({id:0,name:'未分配部门用户',title:'未分配部门用户',value:0});
        if($("#isMany").val() == '0'){
            dept_user_setting.check.chkStyle = 'radio';
            for(var i = 0; i < deptList.length; i++)
                deptList[i].chkDisabled = true;
        }
        var tree = $.fn.zTree.init($("#deptUserTree"),dept_user_setting,deptList);

        loadUserList();
    }

    /**
     * 提交返回数据
     */
    function setSubmitClick(){
        $("#userSubmit").click(function(){
            if(callBackFun && typeof (callBackFun) == 'function'){
                var deptTree = $.fn.zTree.getZTreeObj("deptUserTree");
                var nodes = deptTree.getCheckedNodes(true);
                var selectList = [];
                for(var i = 0; i < nodes.length; i++){
                    var node = nodes[i];
                    if((node.id + '').indexOf('user_') > -1){
                        node = commonTools.clone(node);
                        node.id = node.id.split('_')[1];
                        selectList.push(node);
                    }
                }
                callBackFun(selectList);
            }
            $("#userWindow").dialog('close');
        });
        $("#userCancle").click(function(){
            $("#userWindow").dialog('close');
        })
    }

    /**
     * 加载用户数据
     */
    function loadUserList(){
        var userIdList = userIds.split(',');
        post(getPath() + 'base/user/list/json',{
            deptIds:$("#userDeptId").val(),
            status:$("#userstatus").val(),
            includeEmptyDept:$("#userincludeEmptyDept").val()
        },function(list){
            for(var i = 0; i < list.length; i++){
                var user = list[i];
                user.pId = user.departmentId;
                user.name = user.nickname;
                user.title = user.nickname;
                user.value = user.id;
                user.id = 'user_' + user.id;
                if(!user.departmentId)
                    user.departmentId = 0;
                if($.inArray(user.value + '', userIdList) > -1){
                    user.checked = true;
                }
            }

            formatDeptUsers(list);
        })
    }

    /**
     * 添加部门子节点
     * @param list
     */
    function formatDeptUsers(list){
        var deptnodes = {};
        var deptTree = $.fn.zTree.getZTreeObj("deptUserTree");
        for(var i = 0; i < list.length; i++){
            var user = list[i];
            if(!deptnodes[user.departmentId])
                deptnodes[user.departmentId] = [];
            deptnodes[user.departmentId].push(user);

        }
        for(var key in deptnodes){
            var parentNode = deptTree.getNodeByParam("id", key);
            deptTree.addNodes(parentNode, deptnodes[key], true);
        }
        //打开被选中项
        var selectnodes = deptTree.getCheckedNodes(true);
        for(var x = 0; x < selectnodes.length; x++){
            var parentNode = selectnodes[x].getParentNode();
            deptTree.expandNode(parentNode, true);
        }
    }


})();
